﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iESELibrary.RC4Crypto
{
	public class Decryption : IDisposable
	{
		#region Constants

		private static string PWD_ANT = "NTPASSWORD";
		private static string PWD_ALM = "LMPASSWORD";
		#endregion

		#region Variables
		private bool disposed = false;
		#endregion

		#region Constructors
		#endregion

		#region Public Static Methods
		public static void Encrypt(ref Byte[] data, Byte[] key)
		{
			Byte[] s = new Byte[256];
			Byte[] k = new Byte[256];
			Byte temp;
			int i, j;

			for (i = 0; i < 256; i++)
			{
				s[i] = (Byte)i;
				k[i] = key[i % key.GetLength(0)];
			}

			j = 0;
			for (i = 0; i < 256; i++)
			{
				j = (j + s[i] + k[i]) % 256;
				temp = s[i];
				s[i] = s[j];
				s[j] = temp;
			}

			i = j = 0;
			for (int x = 0; x < data.GetLength(0); x++)
			{
				i = (i + 1) % 256;
				j = (j + s[i]) % 256;
				temp = s[i];
				s[i] = s[j];
				s[j] = temp;
				int t = (s[i] + s[j]) % 256;
				data[x] ^= s[t];
			}
		}

		public static void Decrypt(ref Byte[] bytes, Byte[] key)
		{
			Encrypt(ref bytes, key);
		}

		#endregion

		#region Public Methods
		/// <summary>
		/// Method decrypts hash (currently only NT) using SysKey
		/// </summary>
		/// <param name="rid">MS AD object RID</param>
		/// <param name="encryptedHash">Encrypted hash value</param>
		/// <returns></returns>
		#endregion

		#region IDisposable Members
		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				if (disposing)
				{

				}

				this.disposed = true;
			}
		}
		#endregion
	}
}
